package com.easylinkin.linkappapi.quality.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.common.service.AppSendMessageService;
import com.easylinkin.linkappapi.common.utils.message.entity.AppSendMessage.model;
import com.easylinkin.linkappapi.messagecenter.entity.MessageCenter;
import com.easylinkin.linkappapi.messagecenter.entity.enumType.messageTypeEnum;
import com.easylinkin.linkappapi.messagecenter.service.MessageCenterService;
import com.easylinkin.linkappapi.quality.dto.QualityQuestionInfoDto;
import com.easylinkin.linkappapi.quality.mapper.QualityQuestionInfoMapper;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author: kan yuanfeng
 * @Date: 2022/10/24 14:05
 * @Description: 安全管理内部消息通知工具
 */
@Component
public class QualityMessageUtils implements ApplicationContextAware {

  private static MessageCenterService messageCenterService;

  private static AppSendMessageService appSendMessageService;

  private static LinkappUserContextProducer linkappUserContextProducer;

  private static QualityQuestionInfoMapper questionInfoMapper;

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    messageCenterService = applicationContext.getBean(MessageCenterService.class);
    appSendMessageService = applicationContext.getBean(AppSendMessageService.class);
    linkappUserContextProducer = applicationContext.getBean(LinkappUserContextProducer.class);
    questionInfoMapper = applicationContext.getBean(QualityQuestionInfoMapper.class);
  }

  public static void saveDealMessage(Long linkId, Boolean isPushJG){
    QualityQuestionInfoDto questionInfoDto = new QualityQuestionInfoDto();
    questionInfoDto.setId(linkId);
    questionInfoDto.setType(1);
    questionInfoDto.setTenantId(linkappUserContextProducer.getCurrent().getTenantId());
    IPage<QualityQuestionInfoDto> list = questionInfoMapper
        .findAccountList(new Page<>(0, 1), questionInfoDto);
    QualityQuestionInfoDto infoDto = list.getRecords().get(0);
    saveMessage("复查",infoDto,infoDto.getReviewerId()+"",isPushJG);
  }

  //fflag true通过 反之不通过
  public static void saveReviewMessage(boolean flag,Long linkId, Boolean isPushJG){
    QualityQuestionInfoDto questionInfoDto = new QualityQuestionInfoDto();
    questionInfoDto.setId(linkId);
    questionInfoDto.setType(1);
    questionInfoDto.setTenantId(linkappUserContextProducer.getCurrent().getTenantId());
    IPage<QualityQuestionInfoDto> list = questionInfoMapper
        .findAccountList(new Page<>(0, 1), questionInfoDto);
    QualityQuestionInfoDto infoDto = list.getRecords().get(0);
    saveMessage("复查"+(flag?"通过":"不通过"),infoDto,infoDto.getRectifierId()+"",isPushJG);
    if (flag){
      saveMessage("复查通过",infoDto,infoDto.getCreator(),isPushJG);
    }
  }

  private static void saveMessage(String title,QualityQuestionInfoDto infoDto,String ownerId, Boolean isPushJG){
    MessageCenter messageCenter = new MessageCenter();
    messageCenter.setTenantId(linkappUserContextProducer.getCurrent().getTenantId());
    messageCenter.setOwnerId(ownerId);
    messageCenter.setType(messageTypeEnum.QUALITY.ordinal());
    messageCenter.setSelf(false);
    messageCenter.setStatus(0);
    messageCenter.setTitle("质量"+title+"提醒");
    messageCenter.setCreateTime(new Date());
    messageCenter.setLinkId(infoDto.getId()+"");
    // 根据隐患记录id获取隐患详细
    if(StringUtils.isBlank(messageCenter.getContent())){
        messageCenter.setContent(infoDto.getProblem().getContent());
    }
    messageCenterService.saveOne(messageCenter);
    if(isPushJG){
      // 手机推送
      appSendMessageService
          .pushMessageToPhone(messageCenter.getId().toString(), model.SECURITY, 0,
              messageCenter.getOwnerId(), messageCenter.getTitle(),
              messageCenter.getContent());
    }
  }

  public static void saveMessage(String title, String ownerId, Long linkId, Boolean isPushJG){
    MessageCenter messageCenter = new MessageCenter();
    messageCenter.setTenantId(linkappUserContextProducer.getCurrent().getTenantId());
    messageCenter.setOwnerId(ownerId);
    messageCenter.setType(messageTypeEnum.QUALITY.ordinal());
    messageCenter.setSelf(false);
    messageCenter.setStatus(0);
    messageCenter.setTitle("质量"+title+"提醒");
    messageCenter.setCreateTime(new Date());
    messageCenter.setLinkId(linkId+"");
    // 根据隐患记录id获取隐患详细
    QualityQuestionInfoDto questionInfoDto = new QualityQuestionInfoDto();
    questionInfoDto.setId(linkId);
    questionInfoDto.setType(1);
    questionInfoDto.setTenantId(linkappUserContextProducer.getCurrent().getTenantId());
    if(StringUtils.isBlank(messageCenter.getContent())){
      IPage<QualityQuestionInfoDto> list = questionInfoMapper
          .findAccountList(new Page<>(0, 1), questionInfoDto);
      if (list.getRecords().size() > 0) {
        QualityQuestionInfoDto infoDto = list.getRecords().get(0);
        messageCenter.setContent(infoDto.getProblem().getContent());
      }
    }
    messageCenterService.saveOne(messageCenter);
    if(isPushJG){
      // 手机推送
      appSendMessageService
          .pushMessageToPhone(messageCenter.getId().toString(), model.SECURITY, 0, ownerId,
              messageCenter.getTitle(),
              messageCenter.getContent());
    }
  }
}
